import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/index.vue'
import Layout from '@/views/layout/index.vue'
import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        {
          path: '/dashboard',
          component: () => import('@/views/dashboard/index.vue')
        },
        {
          path: '/transit/freight-manage',
          component: () => import('@/views/transit/freight-manage.vue')
        },
        {
          path: '/branches/organization-manage',
          component: () => import('@/views/branches/organization-manage.vue')
        },
        {
          path: '/branches/institutions-jobs-area',
          component: () => import('@/views/institutions/institutions-jobs-area.vue')
        },
        {
          path: '/transit/vehicle',
          component: () => import('@/views/transitqa/vehicle.vue')
        },
        {
          path: '/transit/car-models',
          component: () => import('@/views/carmodels/index.vue')
        },
        {
          path: '/transit/vehicle-detail',
          component: () => import('@/views/transitqa/details.vue')
        },
        {
          path: '/transit/car-register',
          component: () => import('@/views/register/carregister.vue')
        },
        {
          path: '/transit/car-register-info',
          component: () => import('@/views/register/carregisterinfo.vue')
        },
        {
          path: '/branches/operational-range',
          component: () => import('@/views/operational/range.vue')
        },
        {
          path: '/branches/MapContent',
          component: () => import('@/views/operational/MapContent.vue')
        },
        {
          path: '/transit/driver',
          component: () => import('@/views/driver/index.vue')
        },
        {
          path: '/transit/driver-detail',
          component: () => import('@/views/driver/detail.vue')
        },
        {
          path: '/transit/workArrange-manage',
          component: () => import('@/views/workArrange/workArrange-manage.vue')
        },
        {
          path: '/transit/workArrange-setting',
          component: () => import('@/views/workArrange/workArrange-setting.vue')
        },
        {
          path: '/business/order-manage',
          component: () => import('@/views/business/order-manage.vue')
        },
        {
          path: '/business/edit-order',
          component: () => import('@/views/business/edit-order.vue')
        },
        {
          path: '/business/order-detail',
          component: () => import('@/views/video.vue')
        },
        {
          path: '/business/waybill-manage',
          component: () => import('@/views/video1.vue')
        },
        {
          path: '/business/waybill',
          component: () => import('@/views/waybill/index.vue')
        },
        {
          path: '/business/waybill-detail',
          component: () => import('@/views/waybill/detail.vue')
        },
        {
          path: '/transport/transport-task',
          component: () => import('@/views/transport/transport-task.vue')
        },
        {
          path: '/transport/line-manage',
          component: () => import('@/views/transport/line-manage.vue')
        },
        {
          path: 'branches/pickUptask',
          component: () => import('@/views/pickUptask/index.vue')
        }
      ]
    }
  ]
})

export default router

const whitelist = ['/login']

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  console.log('beforeEach')
  // 如果有token，去登录页就重定向到首页，其他放行。
  // 没有token，去白名单放行，其他重定向到登录
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
router.beforeResolve((to, from, next) => {
  console.log('beforeResolve')
  next()
})
router.afterEach((to, from) => {
  console.log('afterEach')
})
